<template>
	<div>
		<div v-if="pageIndex == 1">
			<div class="module-header">		
				<div class="module-prompt f-left">考试列表</div>
				<el-button class="f-right" type="danger" size="small" @click="openDialog">新增考试</el-button>
			</div>
			<el-table
			stripe
			ref="multipleTable"
			:data="examList"
			tooltip-effect="dark"
			style="width: 100%">
				<el-table-column
				v-for="(data,index) in examOpts"
				:key="index"
				:prop="data.field"
				:label="data.th"
				:formatter="data.format"
				align="center"
				:show-overflow-tooltip="true"
				:width="data.width">
				</el-table-column>		    
				<el-table-column
				label="操作"
				align="center"
				width="200">
				<template slot-scope="scope">
					<el-button type="text" size="mini" @click="handleSet(scope.row)">设置</el-button>
					<el-button type="text" size="mini" @click="handleTask(scope.row)">任务</el-button>
<!-- 					<el-button type="text" size="mini" @click="handleDetail(scope.row)">详情</el-button>
					<el-button type="text" size="mini" @click="handleDel(scope.row)">删除</el-button> -->
					<el-button type="text" size="mini" @click="openDel(scope.row)">删除</el-button>
				</template>
				</el-table-column>
			</el-table>
			<el-row :gutter="20" class="p-t-10" v-if="pageCount>1">
			  <el-col :span="10" :offset="14">
			  	<el-pagination
				  class="f-right"
				  @current-change="handleCurrentChange"
			      :current-page.sync="searchObj.page"
			      :page-size="searchObj.size"
			      :page-count="pageCount" 
				  background
				  layout="prev, pager, next">
				</el-pagination>
			  </el-col>
			</el-row>
			<el-dialog :title="'考试' + (mode=='add' ? '录入' : '编辑')" width='60%' :visible.sync="dialogVisible">
			  <div class='over-hid'>
					<v-input sm="13" label="考试名称">
						<el-input v-model="examObj.title" placeholder="考试名称" size="small"></el-input>
					</v-input>
					<v-input sm="16" label="考试描述">
						<el-input type="textarea" v-model="examObj.description" placeholder="考试描述" size="small"></el-input>
					</v-input>	
					<!-- <div>{{examObj.setting}}</div>		 -->
					<v-input sm="24" label="考题设置" class="setting">
						<el-cascader
						 	size="small"
						 	placeholder="添加分类"
						    :options="options"
						    v-model="categoryTypes"
						    clearable
						    @change="handleChange" 
							>
						</el-cascader>
						<div class="set-list" v-for="(item,index) in examObj.setting">
							<el-tag
							  class="m-b-10"
							  :key="item.categoryName"
							  closable
							  @close="handleDel(index)">
							  {{item.categoryName}}
							</el-tag>
							<el-row :gutter="10">
								<el-col :xs="24" :sm="8" :lg="8" v-for="(item1,index1) in item.types" :key="index1">
									<el-input min="1" size="small" class="m-b-10" type="number" :max="item1.count" placeholder="题数" v-model.number="item1.num" @input="refreshScore++">
									    <template slot="prepend">{{formatType(item1.type)}}数量 <span class="text-danger"  v-if="item1.count">({{item1.count}})</span></template>
									</el-input>
								</el-col>
							</el-row>
						</div>
					</v-input>			
					<v-input sm="24" label="分值设置">
						<el-row :gutter="10">
							<el-col :xs="24" :sm="8" :lg="8">
								<el-input min="1" size="small" @input="refreshScore++" class="m-b-10" type="number" placeholder="单选题分数" v-model.number="scoreSetting.singleScore">
								    <template slot="prepend">单选题分值</template>
								</el-input>
							</el-col>
							<el-col :xs="24" :sm="8" :lg="8">
								<el-input min="1" size="small" @input="refreshScore++" class="m-b-10" type="number" placeholder="多选题分数" v-model.number="scoreSetting.multipleScore">
								    <template slot="prepend">多选题分值</template>
								</el-input>
							</el-col>
							<el-col :xs="24" :sm="8" :lg="8">
								<el-input min="1" size="small" @input="refreshScore++" class="m-b-10" type="number" placeholder="判断题分数" v-model.number="scoreSetting.judgeScore">
								    <template slot="prepend">判断题分值</template>
								</el-input>
							</el-col>
							<el-col :xs="24" :sm="8" :lg="8">
								<el-input min="1" size="small" @input="refreshScore++" class="m-b-10" type="number" placeholder="填空题分数" v-model.number="scoreSetting.gapScore">
								    <template slot="prepend">填空题分值</template>
								</el-input>
							</el-col>
						</el-row>
					</v-input>	
					<v-input sm="12" label="总分">
						<el-input size="small" disabled v-model="totalScore" placeholder="请输入内容"></el-input>
					</v-input>
					<v-input sm="12" label="及格分">
						<el-input min="1" size="small" v-model.number="examObj.pass" type="number" placeholder="及格分数"></el-input>
					</v-input>
			  </div>
			  <div slot="footer" class="dialog-footer">
			    <el-button size="small" @click="dialogVisible = false">关闭</el-button>
			    <el-button size="small" type="danger" @click="mode == 'add' ? addExam() : editExam()">{{mode == "add" ? "新增考试" : "更新考试"}}</el-button>
			  </div>
			</el-dialog>			
		</div>
		<task-list v-else-if="pageIndex == 2" :index.sync="pageIndex" :data="selectData"></task-list>
	</div>
</template>
<script>
	import{
		vInput,
		vSingleImg
	}from "vHome"
	import{
		examOpts
	}from "./exam"
	import{
		userService
	}from "api"
	import taskList from "./taskList"
	export default{
		components:{
			vInput,
			vSingleImg,
			taskList
		},
		data(){
			return {
				pageIndex: 1,
				examOpts,
				searchObj:{
					page: 1,
					size: 5
				},
				pageCount: 0,
				examObj: {},
				scoreSetting: {},
				mode: "add",
				dialogVisible: false,
				dialogVisible1: false,
				examList: [{}],
				categoryTypes:[],
				category: [],
				options: [],
				categoryList:[],
				refreshList: 0,
				totalScore: 0,
				taskObj: {},
				selectData: {},
				typeList: ["单选题","多选题","判断题","填空题"],
				refreshScore: 0,

			}
		},
		methods:{
			formatType(type){
				return this.typeList[type-1]
			},
			getCategoryList(){
				userService.getCategoryList()
				.then(res => {
                 //console.log(res)
					let categoryList = res
					let options = []
					categoryList.forEach((item,index) => {
						//console.log(item)
						let children = []
						if(item.children){
							item.children.forEach((item1,index1) => {
								if(item1.question){
									children.push({
										value: item1.id+"-"+item1.name,
										label: item1.name +"(" + item1.question + ")",
										id: item1.id
									})									
								}
							})
						}
						options.push({
							value: item.name,
							label: item.name +"(" + item.question + ")",
							children: children,
							id: item.id
						})
					})
					this.options = options
				})
			},

			handleDel(index){
				this.examObj.setting.splice(index,1)
				this.calScore()
			},
			openDel(row) {
                this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
                   confirmButtonText: '确定',
                   cancelButtonText: '取消',
                   type: 'warning'
                }).then(() => {
                    row.delSet=false;
                    console.log(row);
                    userService.delDisExam(row.id, row.delSet)
                    .then(res => {
                        this.$message({
                        type: 'success',
                        message: '删除成功!'
						});
					this.dialogVisible = false
					this.refreshList++
                      })
                   }).catch(() => {
                      this.$message({
                      type: 'info',
                      message: '已取消删除'
                      });          
                    });
             },
			handleChange(value){
			    //console.log(value)
				//debugger;
				// let name = this.options[value[0]].label+"/"+this.options[value[0]].children[value[1]].label;
				 let arr=value[1].split("-");
				 let name = value[0]+"/"+arr[1]||"";
				//  let id = this.options[value[0]].children[value[1]].id
				 let id = parseInt(arr[0]);
				 userService.getCategoryInfo(id)
			 .then(res => {
                     console.log(res);
				 	// if(res.successcs)
				 	let types = res

					// if(types.length == 0) return this.$notify({
				  //        message: '请填写任务名称',
				  //        type: 'warning'
				 //    })
					types.forEach(item => {
				 		item.num = 0
					})
					this.examObj.setting.push({
						category: id,
						categoryName: name,
						types: types
				 	})					
				 })

			},
			handleTask(row){
				this.pageIndex = 2
				this.selectData = row
			},

			handleSet(row){
				this.dialogVisible = true
				this.examObj = row
				this.mode = "edit"
				this.scoreSetting = row.scoreSetting
				this.refreshScore++ 
			},
			openDialog(){
				this.dialogVisible = true
				this.examObj = {
					setting: []
				}
			},
			handleCurrentChange(){

			},
			handleDetail(){

			},
			addExam(){
				this.examObj.scoreSetting = this.scoreSetting
				this.examObj.delset = true
				this.examObj = this.countExamNum(this.examObj);
				userService.addExam(this.examObj)
				.then(res => {
					this.dialogVisible = false
					this.refreshList++
				})
			},
			editExam(){
				this.examObj.scoreSetting = this.scoreSetting;
				this.examObj = this.countExamNum(this.examObj);
				userService.editExam(this.examObj.id,this.examObj)
				.then(res => {
					this.dialogVisible = false
					this.refreshList++
				})
			},
			countExamNum(examObj){
				let judgeNum = 0, singleNum = 0, multipleNum = 0, gapNum=0, totals = 0;
				examObj.setting && examObj.setting.forEach( item => {
					if(item.types)
						item.types.forEach(obj => {
						if(obj.type == 1){
							singleNum += obj.num
						}else if(obj.type == 2){
							multipleNum += obj.num
						}else if(obj.type == 3){
							judgeNum += obj.num
						}else if(obj.type == 4){
							gapNum += obj.num
						}
					})
				})
			   totals = (judgeNum + singleNum + multipleNum+gapNum);
			   examObj.quesNum = {"judgeNum": judgeNum, "singleNum": singleNum, "multipleNum": multipleNum,"gapNum":gapNum,"totals": totals};
			   return examObj;
			},
			getExamList(){
				debugger;
				userService.getExamList()
				.then(res => {	
					debugger;
                    let examLs=[];
					for (let index = 0; index < res.content.length; index++) {
                      if(res.content[index].delset==true){
					   examLs.push(res.content[index]);
                     }    
					 }
					 this.examList=examLs;
				})
			},
			calScore(){
				let totalScore = 0
				let setArr = this.examObj.setting
				let scoreSet = this.scoreSetting
				setArr.forEach(item => {
                    for(let i=0;i<item.types.length;i++){
                           if(item.types[i].type==1){
							   totalScore += item.types[i].num * scoreSet.singleScore
						   }
						   if(item.types[i].type==2){
							   totalScore += item.types[i].num * scoreSet.multipleScore
						   }
						   if(item.types[i].type==3){
							   totalScore += item.types[i].num * scoreSet.judgeScore
						   }
						   if(item.types[i].type==4){
							   totalScore += item.types[i].num * scoreSet.gapScore
						   }
					}

                       /*if(item.types.length==3){
					       totalScore += item.types[0].num * scoreSet.singleScore
					         totalScore += item.types[1].num * scoreSet.multipleScore
							 totalScore += item.types[2].num * scoreSet.judgeScore
					    }
						if(item.types.length==2){
					 	   if(item.types[0].type==1&&item.types[1].type==2){
					  	     totalScore += item.types[0].num * scoreSet.singleScore
					           totalScore += item.types[1].num * scoreSet.multipleScore 
					 	   }else if(item.types[0].type==1&&item.types[1].type==3){
                               totalScore += item.types[0].num * scoreSet.singleScore
					           totalScore += item.types[1].num * scoreSet.judgeScore
					  	   }else if(item.types[0].type==2&&item.types[1].type==3){
                               totalScore += item.types[0].num * scoreSet.multipleScore
					          totalScore += item.types[1].num * scoreSet.judgeScore
					 	   }
					 	  // console.log(totalScore)
					     }
						 if(item.types.length==1){
                           if(item.types[0].type==1){
                              totalScore += item.types[0].num * scoreSet.singleScore
					  	  }
					  	  if(item.types[0].type==2){
                             totalScore += item.types[0].num * scoreSet.multipleScore
					  	  }
					 	  if(item.types[0].type==3){
                              totalScore += item.types[0].num * scoreSet.judgeScore
					 	  }
					    }	*/				
				})
				this.totalScore = totalScore
			}
		},
		mounted(){
			this.refreshList++
			this.getCategoryList()
		},
		watch:{
			"refreshList":"getExamList",
			"refreshScore": "calScore" 
		}
	}
</script>
<style scoped>
	.setting{
		position: relative;
	}
	.set-list{
		margin-top: 10px;
	}
</style>